package weixin02.service.impl;

import weixin02.entity.Goods;
import weixin02.dao.GoodsDao;
import weixin02.query.LimitQuery;
import weixin02.service.GoodsService;
import org.springframework.stereotype.Service;
import weixin02.util.R;
import weixin02.util.ResultCode;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Goods)表服务实现类
 *
 * @author makejava
 * @since 2021-07-06 21:14:22
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsDao goodsDao;

    /*分页查询所有商品信息*/
    @Override
    public R queryAllByLimit(LimitQuery query) {
        Long page = query.getPage(); //当前页数
        Long limit = query.getLimit(); //每页条数
        if(page<1){
            page = 1L;
        }
        Long offset = (page-1)*limit;
        List<Goods> goods = goodsDao.queryAllByLimit(offset, limit);
        //查询总数据个数
        Long count = goodsDao.queryCount();
        return new R(goods, ResultCode.SUCCESS,count);
    }

    /*查询所有商品信息*/
    @Override
    public R queryAll() {
        List<Goods> goods = goodsDao.queryAll(new Goods());
        return new R(goods, ResultCode.SUCCESS);
    }

    /*根据类型查询商品*/
    @Override
    public R queryGoodsByType(int goodsType) {
            Goods goods = new Goods();
            goods.setTypeId(goodsType);
            List<Goods> goods1 = goodsDao.queryAll(goods);
            return new R(goods1, ResultCode.SUCCESS);
    }

    /*根据名称查询商品*/
    @Override
    public R queryGoodsByName(String goodsName) {
        Goods goods = new Goods();
        goods.setGoodsName(goodsName);
        List<Goods> goods1 = goodsDao.queryAll(goods);
        return new R(goods1, ResultCode.SUCCESS);
    }

    /*根据ID查询商品*/
    @Override
    public R queryById(Integer id) {
        Goods goods = goodsDao.queryById(id);
        return new R(goods, ResultCode.SUCCESS);
    }

    /*新增商品*/
    @Override
    public R insert(Goods goods) {
        int rows = this.goodsDao.insert(goods);
        return new R(rows, ResultCode.SUCCESS);
    }

    /*更改商品信息*/
    @Override
    public R update(Goods goods) {
        int rows = this.goodsDao.update(goods);
        return  new R(rows, ResultCode.SUCCESS);
    }


    /*删除商品*/
    @Override
    public R deleteById(Integer id) {
        int rows = goodsDao.deleteById(id);
        return new R(rows, ResultCode.SUCCESS);
    }

}
